package com.jinke.api.modules.app.mapper;

import com.jinke.api.modules.app.controller.param.CompanySellerProfitParam;
import com.jinke.api.modules.app.model.Order;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jinke.api.modules.app.model.OrderParcel;
import com.jinke.api.modules.app.response.UserOrderCountVO;
import com.jinke.api.modules.app.response.UserOrderSummaryVO;
import com.jinke.api.modules.ums.dto.UserDayOrderProfitDto;
import com.jinke.api.modules.ums.dto.UserMonthStatisticsDto;
import com.jinke.api.modules.ums.dto.UserSumAmountDto;
import com.jinke.api.modules.ums.dto.UserWeekSumAmountDto;
import com.jinke.api.modules.ums.model.DailyOrderAmountGroupbySellerVO;
import com.jinke.api.modules.ums.model.DailyOrderAmountVO;
import com.jinke.api.modules.ums.model.UmsAdmin;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 承运商平台 Mapper 接口
 * </p>
 *
 * @author macro
 * @since 2024-01-15
 */
public interface OrderMapper extends BaseMapper<Order> {

    /**
     * 查询当前用户取消订单中的金额
     *
     * @param id 用户id
     * @return 金额
     */
    BigDecimal sumRefundAmountByUserId(@Param("id") Integer id);

    List<UserSumAmountDto> getTopAmountByUserDate(@Param("normalStatus") List<Integer> orderNormalStatus);

    /*List<UserSumAmountDto> getOrderNumByUsers(@Param("normalStatus") List<Integer> orderNormalStatus, @Param("userIds") List<Long> userIds);*/

    List<UserSumAmountDto> getOrderAmountSum(@Param("userId") Integer userId, @Param("normalStatus") List<Integer> orderNormalStatus);

    List<UserSumAmountDto> getUserAmountNum(@Param("sellerId") Integer sellerId, @Param("normalStatus") List<Integer> orderNormalStatus);

    /**
     *  每日订单数+金额+成本统计
     * @param startTime
     * @param endTime
     * @return
     */
    List<DailyOrderAmountVO> getDailyOrderQuantity(@Param("startTime") String startTime, @Param("endTime") String endTime);

    List<UserWeekSumAmountDto> getWeekTopAmountByUserDate(@Param("userIds") List<Long> userIds, @Param("normalStatus") List<Integer> orderNormalStatus);

    List<Long> getTopAmountUserByThisWeek(@Param("normalStatus") List<Integer> orderNormalStatus);

    List<UserWeekSumAmountDto> getUserWeekAmountNum(@Param("sellerId") Integer sellerId, @Param("normalStatus") List<Integer> orderNormalStatus);

    List<UserWeekSumAmountDto> getWeekAmountNum(@Param("userId") Integer userId, @Param("normalStatus") List<Integer> orderNormalStatus);
    /**
     * 按月销售名下每日订单数
     * @param startTime
     * @param endTime
     * @return
     */
    List<DailyOrderAmountGroupbySellerVO> dailyOrderAmountGroupbySellerVO(@Param("startTime") String startTime, @Param("endTime") String endTime);

    List<UserWeekSumAmountDto> getWeekTopRateByUserEveryDay(@Param("userIds") List<Long> userIds, @Param("normalStatus") List<Integer> orderNormalStatus,
                                                            @Param("startTime") String startTime, @Param("endTime") String endTime);

    List<UmsAdmin> getTopAmountUserByCurDay(@Param("normalStatus") List<Integer> orderNormalStatus);

    List<UserWeekSumAmountDto> getUserWeekDayAmountNum(@Param("users") List<UmsAdmin> collectUsers, @Param("normalStatus") List<Integer> orderNormalStatus,
                                                       @Param("startTime") String startTime, @Param("endTime") String endTime);

    List<UserWeekSumAmountDto> getWeekDayAmountNum(@Param("userId") Integer userId, @Param("normalStatus") List<Integer> orderNormalStatus,
                                                   @Param("startTime") String startTime, @Param("endTime") String endTime);

    List<UserDayOrderProfitDto> getAllSellerUserDay(@Param("users") List<UmsAdmin> allSellerUsers, @Param("normalStatus") List<Integer> orderNormalStatus);

    List<UserMonthStatisticsDto> getUserMonthStatistics(@Param("companySellerProfitParam") CompanySellerProfitParam choseMonth, @Param("normalStatus") List<Integer> orderNormalStatus);

    List<UmsAdmin> getTopAmountUserBySellerCurDay(@Param("users") List<UmsAdmin> sellerUsers, @Param("normalStatus") List<Integer> orderNormalStatus);

    List<UserMonthStatisticsDto> getSellerPerformanceStatisticsBySId(@Param("companySellerProfitParam") CompanySellerProfitParam choseMonth, @Param("normalStatus") List<Integer> orderNormalStatus);

    List<UserMonthStatisticsDto> getSingleSellerPerformanceStatisticsBySId(@Param("companySellerProfitParam") CompanySellerProfitParam choseMonth, @Param("normalStatus") List<Integer> orderNormalStatus, @Param("sellerId")  Integer wzId);

    List<DailyOrderAmountGroupbySellerVO> dailyOrderAmountGroupbySingleSellerVO(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("sellerId") Integer sellerId);

    @ResultType(value = UserOrderCountVO.class)
    @Select({"<script>" ,
            " SELECT uo.user_id, count(uo.user_id) orderCount " ,
            " FROM user_order uo " ,
            "<where>",
            " <if test ='startDay!=null and startDay neq \"\"'>" ,
            " AND uo.created_at &gt;= #{startDay} ",
            " </if>",
            " <if test ='dayEnd!=null and dayEnd neq \"\"'>" ,
            " AND uo.created_at &lt;= #{dayEnd} ",
            " </if>",
            " <if test='userIds !=null '>",
            " AND uo.user_id IN ",
            "       <foreach collection='userIds' item='userId' open='(' separator=',' close=')'>",
            "           #{userId}",
            "       </foreach>" ,
            "</if>",
            "</where>",
            "   GROUP BY uo.user_id",
            "</script>"})
    List<UserOrderCountVO> countGroupByUserId(@Param("userIds") Set<Integer> userIds, @Param("startDay") String startDay, @Param("dayEnd") String dayEnd);

//





//       "<if test='userId!=null and userId neq \"\"'>",
//               "AND uo.user_id = #{userId}",
//               "</if>",

    @ResultType(value = UserOrderSummaryVO.class)
    @Select({"<script>",
            "SELECT uo.user_id, DATE(uo.create_time) AS order_date, SUM(CASE WHEN uo.status = 1 THEN 1 ELSE 0 END) AS no_info_count, COUNT(uo.id) AS total_order_count, ",
            "CASE WHEN COUNT(uo.id) = 0 THEN 0 ELSE SUM(CASE WHEN uo.status = 1 THEN 1 ELSE 0 END) / COUNT(uo.id) * 100 END AS no_info_percentage ",
            "FROM user_order uo ",
            "<where>",
            " AND (uo.status in (1, 2, 3,7))",
            "<if test='startDay!=null and startDay neq \"\"'>",
            "AND uo.create_time &gt;= #{startDay}",
            "</if>",
            "<if test='dayEnd!=null and dayEnd neq \"\"'>",
            "AND uo.create_time &lt;= #{dayEnd}",
            "</if>",


            "<if test='userId!=null and userId.size>0'>",
            "AND uo.user_id IN",
            "<foreach collection='userId' item='user_id' open='(' separator=',' close=')'>",
            "#{user_id}",
            "</foreach>",
            "</if>",


            "</where>",
            "GROUP BY uo.user_id, DATE(uo.create_time)",
            "HAVING no_info_count > 0",
            "order by order_date;",
            "</script>"})
    List<UserOrderSummaryVO> countGroupByDate(@Param("startDay") String startDay, @Param("dayEnd") String dayEnd, @Param("userId") List<Integer> userId);


    /**
     * 查1-10号所有有轨迹的物流单号
     */
    @ResultType(value = OrderParcel.class)
    @Select({"<script>",
            "SELECT up.tracking_number, up.created_at ",
            "FROM user_order_parcel up ",
            "<where>",
            " AND up.order_id IN ( ",
            " SELECT uo.id ",
            " FROM user_order uo ",
            "<where>",
            " AND uo.create_time &gt;= #{startDay}",
            " AND uo.create_time &lt;= #{dayEnd}",
            " AND uo.status in (2,3,7)",
            "<if test='userId!=null and userId.size>0'>",
            " AND uo.user_id IN",
            "<foreach collection='userId' item='user_id' open='(' separator=',' close=')'>",
            "#{user_id}",
            "</foreach>",
            "</if>",
            "</where>",
            " )",
            "</where>",
            "ORDER BY up.created_at;",
            "</script>"})
    List<OrderParcel> selectUserOrderParcels(@Param("startDay") String startDay, @Param("dayEnd") String dayEnd, @Param("userId") List<Integer> userId);
}
